package com.xiaxiang.xiawan.Home.Adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaxiang.xiawan.Home.HomeData;
import com.xiaxiang.xiawan.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gz on 2016/9/11.
 */
public class ShowreelAdapter extends BaseAdapter{
    private Context mContext;
    private LayoutInflater layoutInflater;
    private String TAG="ShowreelAdapter";
    private Activity curActivity;
    public ArrayList<HomeData> mHomeDataList;
    public ShowreelAdapter(Context context,ArrayList<HomeData> dataList) {
        layoutInflater = LayoutInflater.from(context);
        mHomeDataList = dataList;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return  mHomeDataList.size() ;
    }

    @Override
    public Object getItem(int i) {
        return mHomeDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mHomeDataList.get(i).hashCode();
    }


    @Override
    public View getView(int  position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        if ( convertView == null ) {
            convertView = layoutInflater.inflate(R.layout.adp_item_showreel, null);
            viewHolder = new ViewHolder();
            viewHolder.mContainer = convertView.findViewById(R.id.Container);
            viewHolder.mIcon = (ImageView)convertView.findViewById(R.id.imgVideoImg);
            viewHolder.mUsername = (TextView)convertView.findViewById(R.id.tvVideoName);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        HomeData data = (HomeData) getItem(position);
        viewHolder.mUsername.setText(data.mName);
        if(data.mIsSelect){
            viewHolder.mContainer.setBackgroundResource(R.drawable.square_bg);
        }else{
            viewHolder.mContainer.setBackgroundResource(R.drawable.fun_photo_square_bg);
        }
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<ImageView>(viewHolder.mIcon );
        ImageView weekImage = imageViewWeakReference.get();
        //  Glide.with(mContext).load(Uri.parse("file://" + data.mEffectThumbnailUrl)).into(weekImage);
        Glide.with(mContext).load(data.mThumbnailUrl).into(weekImage);
        return convertView;
    }
    private class ViewHolder {
        public ImageView mIcon;
        public TextView mUsername;
        public CheckBox mCheckBox;
        public View mContainer;
    }

    public void addHomeDataByChange(HomeData data){
        mHomeDataList.add(data);
      //  notifyDataSetChanged();
    }

    public void addHomeData(HomeData data){
        mHomeDataList.add(data);
        notifyDataSetChanged();
    }
}
